package cn.signalpha.modules.com.wrapper;

import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.SpringUtil;
import cn.signalpha.core.tool.utils.StringUtil;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.entity.UserTag;
import cn.signalpha.modules.com.service.UserTagService;
import cn.signalpha.modules.com.service.UserWalletService;
import cn.signalpha.modules.com.vo.UserVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 */
public class UserWrapper extends BaseEntityWrapper<User, UserVO> {

	private static final UserTagService userTagService;
	private static final UserWalletService userWalletService;

	static {
		userTagService = SpringUtil.getBean(UserTagService.class);
		userWalletService = SpringUtil.getBean(UserWalletService.class);
	}

	public static UserWrapper build() {
		return new UserWrapper();
	}

	@Override
	public UserVO entityVO(User user) {
		UserVO userVO = Objects.requireNonNull(BeanUtil.copy(user, UserVO.class), MsgConstant.ACCOUNT_NULL);
		if(StringUtil.isNotBlank(user.getTagId())){
			userVO.setUserTagList(userTagService.list(Wrappers.<UserTag>lambdaQuery().in(UserTag::getId, Func.toLongList(userVO.getTagId())).orderByDesc(UserTag::getSort)));
		}
		return userVO;
	}

}
